from optparse import OptionParser
from resource_packer import pack, unpack
import sys

def main():
    usage = """Pack files contained in a directory in a python module
    usage: %prog [-u -r -i -e -d -x] directory module"""
    parser = OptionParser(usage=usage, version="%prog 1.0")

    parser.add_option("-u", "--unpack",
                      action="store_true", dest="unpack", default=False,
                      help="unpack a python module to a directory")

    parser.add_option("-r", "--recurse-subdirectories",
                      action="store_true", dest="recurse", default=False,
                      help="pack subdirectories and their content")

    parser.add_option("-i", "--include-files", action="store", type="string", dest="include_files", default="*",
                      help="pack only files matching this pattern. Accept a comma separed list ")

    parser.add_option("-e", "--exclude-files", action="store", type="string", dest="exclude_files", default="",
                      help="exclude from pack files matching this pattern. Accept a comma separed list ")

    parser.add_option("-d", "--include-dirs", action="store", type="string", dest="include_dirs", default="*",
                      help="pack only directories matching this pattern. Accept a comma separed list ")

    parser.add_option("-x", "--exclude-dirs", action="store", type="string", dest="exclude_dirs", default="",
                      help="exclude from pack directories matching this pattern. Accept a comma separed list ")


    (options, args) = parser.parse_args()
      
    if len(args) != 2:
        parser.error("incorrect number of arguments")

    directory = args[0]
    mod_name = args[1]

    include_files = options.include_files.split(",")
    exclude_files = options.exclude_files.split(",")
    include_dirs = options.include_dirs.split(",")
    exclude_dirs = options.exclude_dirs.split(",")
    
    if options.unpack:
        unpack(directory,mod_name)
        print "%s unpacked in %s" % (mod_name,directory)
    else:
        pack(directory,mod_name,include_files,exclude_files,include_dirs,exclude_dirs,options.recurse)
        print "%s packed in %s" % (directory,mod_name)

if __name__=="__main__":
    main()
